package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 上元堂网上药店
 * @author tngou@tngou.net
 *
 */
public class CN_Syt extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(CN_Syt.class);

	private String url="http://www.syt.cn/product-{}.html";

	


	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.text().indexOf("此商品已经下架")>0)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		json.put("un",url.toString().split("-")[1].replaceAll(".html", "-").split("-")[0]);
		//存储标签
		//标签
		Elements crumbs = doc.getElementsByClass("crumbs");
		if(crumbs.first()!=null)
		{
			Elements as = crumbs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
				as.forEach(e->{
						if(!"首页".equals(e.text())&&!"收藏".equals(e.text())&&StringUtil.isNotEmpty(e.text()))
						{
							list.add(e.text());
						}	
					});
					json.put("tag", list);
		}	
		
		//类型
		Elements proMsg = doc.getElementsByClass("proMsg");
		if(proMsg.first()!=null)
		{
			Element img = proMsg.first().getElementsByTag("img").first();
			if(img!=null)
			{
				
				String alt = img.attr("alt").toUpperCase();
				if(StringUtil.isNotEmpty(alt))
				{
					if(alt.indexOf("RX")>=0)
					{
						json.put("ptype", Ptype.RX处方药.ordinal());
					}if(alt.indexOf("OTC")>=0)
					{
						json.put("ptype", Ptype.OTC非处方甲类.ordinal());
						if(alt.indexOf("乙")>=0)
							json.put("ptype", Ptype.OTC非处方乙类.ordinal());
					}
				}
				
			}
		}
		//图片
		Elements proSmallImgs = doc.getElementsByClass("proSmallImgs");
		if(proSmallImgs.first()!=null)
		{
			Elements imgs = proSmallImgs.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//价格
		Elements price_option = doc.getElementsByClass("price_option");
		if(price_option.first()!=null)
		{
			String s_price = price_option.first().attr("s_price");
			
			json.put("price", getPrice(s_price));
			
		}
		//说明书
		Elements proContent = doc.getElementsByClass("proContent");
		if(proContent.first()!=null)
		{
			JSONObject instructions = instructions_s(proContent.first().getElementsByTag("p").text());
			json.put("instructions",instructions );
			
		}		
		return json;
	}
		
}
